{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is code to process the notebooks in `/examples`. It should be turned into a script at some point. It updates README.md and examples.md"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import nbformat\n",
    "from nbconvert import RSTExporter\n",
    "from os import listdir\n",
    "from os.path import splitext\n",
    "\n",
    "rst_exporter = RSTExporter()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def read_example(fname):\n",
    "    path = \"../examples/\" + fname + \".ipynb\"\n",
    "    nb1 = nbformat.read(file(path), as_version=4)\n",
    "    cells = nb1['cells']\n",
    "    title = cells[0]['source']\n",
    "    text = cells[2]['source']\n",
    "    \n",
    "    (_, resources) = rst_exporter.from_notebook_node(nb1)\n",
    "    outs = resources['outputs']\n",
    "    if len(outs) > 0:\n",
    "        last_res = outs.keys()[-1]\n",
    "        last_image = outs[last_res]\n",
    "    else:\n",
    "        last_image = None\n",
    "    \n",
    "    return title, text, last_image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "frontpage_examples = ['Clustering_Comparison.ipynb',\n",
    " 'Density_Estimation_Julia.ipynb',\n",
    " 'Classifier_Comparison_Julia.ipynb',\n",
    " 'Outlier_Detection.ipynb',\n",
    " 'Plot_Kmeans_Digits.ipynb',\n",
    " 'RBM.ipynb',\n",
    " 'Simple_1D_Kernel_Density.ipynb',\n",
    " 'Text_Feature_Extraction.ipynb',\n",
    " 'Two_Class_Adaboost.ipynb',\n",
    " 'Underfitting_vs_Overfitting.ipynb']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "root = \"https://github.com/cstjean/ScikitLearn.jl/tree/master\"\n",
    "img_markup = \"\"\" <a href=\"{root}/examples/{nb_name}.ipynb\"><img src=\"{root}/docs/example_images/{img_name}.png\" alt=\"{title}\" width=\"170\"> </a> \"\"\"\n",
    "readme_text = file(\"../docs/README_text.md\").read()\n",
    "with file(\"../README.md\", \"w\") as readme, file(\"../docs/examples.md\", \"w\") as examples:\n",
    "    examples.write(\"The examples below are written in Julia, there are many more [in Python](http://scikit-learn.org/stable/auto_examples/index.html). Most examples use Python models, those that use Julia models are marked as such. \\n\\n\")\n",
    "    examples.write(\"Example | Description\\n\")\n",
    "    examples.write(\"---|---\\n\")\n",
    "    for filename in listdir(\"../examples/\"):\n",
    "        fname, ext = splitext(filename)\n",
    "        if ext == \".ipynb\":\n",
    "            title, text, last_image = read_example(fname)\n",
    "            if last_image:\n",
    "                # Use the last_image for the gallery\n",
    "                with file(\"../docs/example_images/\"+fname+\".png\", \"wb\") as image_f:\n",
    "                    image_f.write(last_image)\n",
    "                img_name = fname\n",
    "            else:\n",
    "                img_name = \"Text_image\"\n",
    "            markup = img_markup.format(root=\"{root}\", nb_name=fname, img_name=img_name, title=title)\n",
    "            text_ascii = \"**\"+title.encode(\"ascii\", \"ignore\")[2:]+\"**: \" + text.encode(\"ascii\", \"ignore\").replace(\"\\n\", \" \")\n",
    "            if filename in frontpage_examples:\n",
    "                readme.write(markup.format(root=\".\"))\n",
    "            examples.write(\"%s | %s\\n\" % (markup.format(root=\"..\"), text_ascii))\n",
    "    readme.write(\"\\n\\n\"+readme_text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Not part of the script, but kinda neat:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#from IPython.display import Image\n",
    "#Image(data=resources['outputs']['output_4_0.png'], format='png')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
